/*
 * @Author: hanxin 
 * @Date: 2021-08-11 21:07:47 
 * @Last Modified by: hanxin
 * @Last Modified time: 2021-08-16 01:27:25
 * 客户端服务类
 */
package service;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

import common.Message;
import common.MessageType;
import common.user;

public class userService {
    private user u = new user();// 新建user登录信息对象用于发送
    private Socket socket1 = null;// socket做成属性在线程类中使用

    /**
     * 根据用户传入的参数构建user对象并发送到服务端判断是否登录成功
     * 
     * @param uid
     * @param passwd
     * @return
     */
    public boolean checkUserLogin(String uid, String passwd) {
        u.setUser(uid);
        u.setPasswd(passwd);
        // 注意序列化时的包结构问题,客户端与服务端之间传输的序列化对象包结构必须一致.
        try {
            socket1 = new Socket(InetAddress.getByName("***.***.***.***"), 11122);
            ObjectOutputStream oos = new ObjectOutputStream(socket1.getOutputStream());
            // 序列化用户对象
            oos.writeObject(u);
            // socket1.shutdownOutput();

            // 反序列化服务端返回的message对象
            ObjectInputStream ois = new ObjectInputStream(socket1.getInputStream());
            Message mess = (Message) ois.readObject();

            if (mess.getMessType().equals(MessageType.MESSAGE_LOGIN_SUCCESS)) {

                // 登录成功后将socket交给线程维护
                clientConnectServerThread ccst = new clientConnectServerThread(socket1);
                ccst.start();

                // 将线程放入hashtable统一管理
                manageClienToServerThread.Threadjoin(uid, ccst);
                return true;
            } else {
                // 登录失败线程未启动,但socket存在.所以要将其关闭
                socket1.close();
            }
        } catch (Exception e) {

            System.err.println("server_not_online_or_network_unstable_or_passwd_incorrect");
        }

        return false;
    }

    /**
     * 注册请求发送方法,用另外一个socket与服务端通讯
     * 
     * @param user
     * @param passwd
     */
    public void register(String user, String passwd) {
        try {
            socket1 = new Socket(InetAddress.getByName("***.***.***.***"), 11123);
            ObjectOutputStream oos = new ObjectOutputStream(socket1.getOutputStream());
            ObjectInputStream ois = new ObjectInputStream(socket1.getInputStream());
            // 创建注册用户对象
            user u = new user();
            u.setUser(user);
            u.setPasswd(passwd);

            oos.writeObject(u);

            Message rMessage = (Message) ois.readObject();
            if (rMessage.getMessType().equals(MessageType.MESSAGE_USER_REGISTER_SUCCESS)) {
                System.out.println("register_success");

                socket1.close();// 注册结束关闭socket

            } else if (rMessage.getMessType().equals(MessageType.MESSAGE_USER_REGISTER_FAIL_1)) {
                System.out.println(rMessage.getContent());
                System.out.println("register_fail");

                socket1.close();
            } else if (rMessage.getMessType().equals(MessageType.MESSAGE_USER_REGISTER_FAIL_2)) {
                System.out.println(rMessage.getContent());
                System.out.println("register_fail");

                socket1.close();

            }

        } catch (Exception e) {
            System.out.println("register_time_out");
            // e.printStackTrace();
        }
    }

    /**
     * 向服务端发送请求获取所有在线用户id
     */
    public void getOnlineLIst() {

        try {
            // 构建Message对象
            Message mess = new Message();
            mess.setMessType(MessageType.MESSAGE_GET_ONLINE_LIST);
            mess.setSender(u.getUser());

            // 在用户登录成功后此用户的socket线程就会根据uid加入线程集合
            // 在线程管理集合中拿到指定uid的线程,再拿到线程类的属性socket.getOutputStream
            ObjectOutputStream oos = new ObjectOutputStream(
                    manageClienToServerThread.returnThread(u.getUser()).getSocket1().getOutputStream());
            oos.writeObject(mess);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 客户端退出服务端登录方法
     */
    public void logOut() {

        try {
            // 构建message对象,发送结束信息到服务端
            Message mess = new Message();
            mess.setMessType(MessageType.MESSAGE_CLIENT_EXIT);
            mess.setSender(u.getUser());

            ObjectOutputStream oos = new ObjectOutputStream(
                    manageClienToServerThread.returnThread(u.getUser()).getSocket1().getOutputStream());
            oos.writeObject(mess);
            // 关闭线程
            manageClienToServerThread.returnThread(u.getUser()).exitSocket();
            manageClienToServerThread.returnThread(u.getUser()).getSocket1().close();
            // 移除线程
            manageClienToServerThread.removeThread(u.getUser());

        } catch (Exception e) {

            e.printStackTrace();
        }
    }

}
